package com.roadjava.GDOUGym.controller.admin;

import com.roadjava.GDOUGym.bean.dto.EquipmentDTO;
import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.bean.res.TableResult;
import com.roadjava.GDOUGym.service.EquipmentService;
import com.roadjava.GDOUGym.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/equipment")
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    //添加器材
    @RequestMapping("/add.do") @ResponseBody
    public ResultDTO<String> addcall(@RequestBody EquipmentDTO dto) {
        try {
            return equipmentService.add(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("添加器材异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("添加器材异常");
        }
    }

    //删除器材
    @RequestMapping("/delete.do") @ResponseBody
    public ResultDTO<String> delete(@RequestBody EquipmentDTO dto) {
        try {
            return equipmentService.delete(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("删除器材异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("删除器材异常");
        }
    }

    //修改器材
    @RequestMapping("/change.do") @ResponseBody
    public ResultDTO<String> change(@RequestBody EquipmentDTO dto) {
        try {
            return equipmentService.change(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("修改器材异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("修改器材异常");
        }
    }

    //查询场地
    @RequestMapping ("find.do") @ResponseBody
    public ResultDTO<TableResult<EquipmentDTO>> find(@RequestBody EquipmentDTO dto){
        try {
            TableResult<EquipmentDTO> tableResult = new TableResult<>();
            // service层返回
            ResultDTO<List<EquipmentDTO>>  siteDTO = equipmentService.find(dto);
            tableResult.setRows(siteDTO.getData());
            tableResult.setTotalCount(siteDTO.getTotal());
            return ResultDTO.buildSuccess(tableResult);
        }catch (Exception e) {
            log.error("查询器材异常",e);
            throw new RuntimeException("查询器材异常");
        }
    }


    //索引场地页面到main页面
    @GetMapping("/toEquipment")
    public String toEquipment() {
        return "admin/admin/equipment";
    }
}
